;(function ($) {
    var defaluts = {select: "select", select_text: "select_text", select_ul: "select_ul"};
    $.fn.extend({
        "select": function (options) {
            var opts = $.extend({}, defaluts, options);
            return this.each(function () {
                var $this = $(this);
                if ($this.data("value") !== undefined && $this.data("value") !== '') {
                    $this.val($this.data("value"));
                }
                var _html = [];
                _html.push("<div class=\"" + $this.attr('class') + "\">");
                _html.push("<div class=\"" + opts.select_text + "\">" + $this.find(":selected").text() + "</div>");
                _html.push("<ul class=\"" + opts.select_ul + "\">");
                $this.children("option").each(function () {
                    var option = $(this);
                    if ($this.data("value") == option.val()) {
                        _html.push("<li class=\"cur\" data-value=\"" + option.val() + "\">" + option.text() + "</li>");
                    } else {
                        _html.push("<li data-value=\"" + option.val() + "\">" + option.text() + "</li>");
                    }
                });
                _html.push("</ul>");
                _html.push("</div>");
                var select = $(_html.join(""));
                var select_text = select.find("." + opts.select_text);
                var select_ul = select.find("." + opts.select_ul);
                $this.after(select);
                $this.hide();
                select.click(function (event) {
                    $(this).find("." + opts.select_ul).slideToggle().end().siblings("div." + opts.select).find("." + opts.select_ul).slideUp();
                    event.stopPropagation();
                });
                $("body").click(function () {
                    select_ul.slideUp();
                });
                select_ul.on("click", "li", function () {
                    var li = $(this);
                    var val = li.addClass("cur").siblings("li").removeClass("cur").end().data("value").toString();
                    if (val !== $this.val()) {
                        select_text.text(li.text());
                        $this.val(val);
                        $this.attr("data-value", val);
                    }

                    if(val.indexOf('server')!==-1) {

                        val = val=='server_sofertusa1_'?'server_sofertusa1':val;

                        switch (val){
                            case 'server_sofertusa3':
                                li.parents('.model').find('.disk').text('240GB SSD');
                                break;
                            case 'server_sofertusa4':
                                li.parents('.model').find('.disk').text('480GB SSD');
                                break;
                        }

                        var arr_ = {'monthly':1,'quarterly':3,'semiannually':6,'annually':12,'biennially':24,'triennially':36};
                        var cycle_arr = {'monthly':'元/月','semiannually':'元/半年','annually':'元/年','biennially':'元/2年','triennially':'元/3年'};

                        var cycle = li.parents('.model').find('.cart_order').attr('cycle');

                        li.parents('.model').find('.cart_order').attr('data-type', val);

                        var protype = li.parents('.model').find('.cart_order').attr('protype');

                        $.ajax({
                            url: 'https://my.henghost.com/activity20200713.php?callback=?',
                            type: "get",
                            data: "action=getConfig&type=" + val + "&billingcycle=" + cycle + "&servertype=" + protype,
                            dataType: "jsonp",
                            success: function (d) {
                                li.parents('.model').find('.bright').html('<p><b>¥<span>'+d.price+'</span></b>'+cycle_arr[cycle]+'</p>');
                                li.parents('.model').find('.price-right').html('<s>'+d['oprice']+cycle_arr[cycle]+'</s><div class="lad-province">省: '+(d['oprice']-d.price)+'元</div>');
                                li.parents('.model').find('.yue').html('约'+(d.price/arr_[cycle]).toFixed(0)+'元/月');
                            }
                        })

                    }

                });
            });
        }
    });
})(jQuery);